package com.wnhz.work.jy.fragment.good;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.GoodDetailsMainActivity;
import com.wnhz.work.jy.adapter.Home3Adapter;
import com.wnhz.work.jy.adapter.PandaAdapter;
import com.wnhz.work.jy.bean.HomeFragment1.RecommendBean;
import com.wnhz.work.jy.bean.HomefragBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class PandaFragment extends Fragment {
    private static final String TAG = "PandaFragment";
    private View view;

    private GridView gridView;
    private Home3Adapter mAdapter;
    private List<RecommendBean> beanList = new ArrayList<>();

    private MyMain myMain;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_derive, container, false);

        myMain = (MyMain) getActivity();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        gridView = (GridView) view.findViewById(R.id.gridView);
        mAdapter = new Home3Adapter(getActivity(),beanList);
        gridView.setAdapter(mAdapter);

//        mHandler.sendEmptyMessage(1);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), GoodDetailsMainActivity.class);
                intent.putExtra("goodId", beanList.get(position).getGoodId());
                startActivity(intent);
            }
        });
        getData("1");
    }


    private void getData(String type){
        Map<String,Object> map = new HashMap<>();

        myMain.showDialog();
        map.put("type",type);
        XUtil.Post(ConfigUtil.GOODS_LIST,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: "+result);
                try {
                    JSONArray array = new JSONArray(result);
                    Gson gson = new Gson();
                    beanList.clear();
                    for (int i = 0; i < array.length(); i++) {
                        RecommendBean bean = new RecommendBean();
                        bean = gson.fromJson(array.optString(i).toString(),RecommendBean.class);
                        beanList.add(bean);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                myMain.closeDialog();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                super.onCancelled(cex);
            }
        });
    }
}
